using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Windows_Explorer.Class;
using System.IO;
using System.Collections;
using System.Windows.Forms.Design;
using Microsoft.VisualBasic.FileIO;
using System.Threading;
//using System.Runtime.InteropServices;
//using System.Runtime;

namespace Windows_Explorer.Forms
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }
        public ListViewItem lviItem;
        public ArrayList Back, Forward; public string cdrom = "";
        private void frmMain_Load(object sender, EventArgs e)
        {
            txtSearch.Hide();
            txtSearch.Text = "Search";
            txtSearch.Font = new Font("Tahoma", 8, FontStyle.Italic);
            txtSearch.BackColor = Color.Bisque;
            try
            {
                mnuDetails.Checked = true;
                detailsToolStripMenuItem.Checked = true;
                viewDetails.Checked = true;
                cboPathSearch.Text = "My Documents";
                pnBase.Hide();
                Back = new ArrayList();
                Forward = new ArrayList();
                Class.clsTreeView.CreateTreeView(tvTreeView);
                tvTreeView.SelectedNode = tvTreeView.Nodes[0];
                TreeNode tn = new TreeNode();
                cboPathSearch.Items.Add("My Documents");
                cboPathSearch.Items.Add("My Pictures");
                cboPathSearch.Items.Add("Desktop");
                cboPathSearch.Items.Add("My Computer");
                foreach (DriveInfo drvi in DriveInfo.GetDrives())
                {
                    TreeNode tndrv = new TreeNode();
                    if (drvi.DriveType == DriveType.CDRom)
                    {
                        tndrv.Text = "    CD Drive (" + drvi.Name + ")";
                        cdrom = tndrv.Text;
                    }
                    else
                    {
                        tndrv.Text = "    " + drvi.Name;
                    }
                    tn.Nodes.Add(tndrv);
                }
                for (int i = 0; i < tn.Nodes.Count; i++)
                {
                    cboPathSearch.Items.Add(tn.Nodes[i].Text.ToString());
                }
                cboPathSearch.Items.Add("Browse...");
                cboPathSearch.Items.Add("Recent :");
            }
            catch { }
        }

        private void tvTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node.Nodes[0].Text == "")
            {
                clsTreeView.Expand(e.Node);
            }
        }
        
        clsTreeView cls = new clsTreeView();
        clsItem clsitem = new clsItem();
        clsHistory History = new clsHistory();
        private void btnUp_Click(object sender, EventArgs e)
        {
            try
            {
                cls.ParentFolder(lvListView, tvTreeView);
                cboPath.Text = cls.Path;
                string[] arr = cboPath.Text.Split('\\');
                string name = arr[arr.Length - 1];
                Program.fmain.Text = name;
                cls.HistoryAdd();
                statusLabel1.Text = lvListView.Items.Count.ToString() + " object(s)";
            }
            catch { }
        }
       
        private void btnGo_Click(object sender, EventArgs e)
        {
            try
            {
                if (cboPath.Text != null)
                {
                    FileInfo f = new FileInfo(cboPath.Text.Trim());
                    if (f.Exists)
                    {
                        System.Diagnostics.Process.Start(cboPath.Text.Trim());
                        DirectoryInfo parent = f.Directory;
                        cboPath.Text = parent.FullName;
                        return;
                    }
                    cls.OpenFolder(cboPath.Text.Trim(), lvListView);
                    cls.HistoryAdd();
                    statusLabel1.Text = lvListView.Items.Count.ToString() + " object(s)"; 
                }
            }
            catch { }
        }

        private void sbtnView_Click(object sender, EventArgs e)
        {
            ToolStripDropDownItem source = (ToolStripDropDownItem)sender;
            switch (source.Text)
            {
                case "Large Icons":
                    lvListView.View = View.LargeIcon;
                    break;
                case "Small Icons":
                    lvListView.View = View.SmallIcon;
                    break;
                case "List":
                    lvListView.View = View.List;
                    break;
                case "Details":
                    lvListView.View = View.Details;
                    break;
                default:
                    break;
            }
        }

        private void sbtnBack_ButtonClick(object sender, EventArgs e)
        {
            try
            {
                string path = cls.HistoryBack(lvListView, tvTreeView.Nodes[0]);
                string[] arr = path.Split('\\');
                string name = arr[arr.Length - 1];
                Program.fmain.Text = name;
                if (path != null)
                {
                    cboPath.Text = path;
                    statusLabel1.Text = lvListView.Items.Count.ToString() + " object(s)";
                    int Count = Back.Count;
                    if (Count > 0)
                    {
                        TreeNode Node = new TreeNode();
                        Node = (TreeNode)Back[Count - 1];
                        Back.RemoveAt(Count - 1);
                        sbtnBack.DropDown.Items.RemoveAt(Count - 1);
                        Forward.Add(Node);
                        History.InsertForward_DropdownButton(Node);
                    }
                }
            }
            catch { }
        }
        
        private void sbtnForward_ButtonClick(object sender, EventArgs e)
        {
            try
            {
                string path = cls.HistoryForward(lvListView, tvTreeView.Nodes[0]);
                string[] arr = path.Split('\\');
                string name = arr[arr.Length - 1];
                Program.fmain.Text = name;
                if (path != null)
                {
                    cboPath.Text = path;
                    statusLabel1.Text = lvListView.Items.Count.ToString() + " object(s)";
                }
            }
            catch { }
        }

        private void tvTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                cls.OpenFolder(e.Node, lvListView);
                if (e.Node.Text.Equals("My Computer") && e.Node.Parent.Text == "Desktop")
                    cboPath.Text = "My Computer";
                else
                    cboPath.Text = cls.Path;
                Program.fmain.Text = e.Node.Text;
                cls.HistoryAdd();
                Back.Add(e.Node);
                statusLabel1.Text = lvListView.Items.Count.ToString() + " object(s)";
                History.InSertBack_DropDownbutton(e.Node);
            }
            catch { }
        }
        private void lvListView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                ListViewItem item = lvListView.FocusedItem;
                cls.OpenFolder(item, lvListView);
                cboPath.Text = cls.Path;
                Program.fmain.Text = item.Text;
                cls.HistoryAdd();
                statusLabel1.Text = lvListView.Items.Count.ToString() + " object(s)";
            }
            catch { }
        }

        private void view_Click(object sender, EventArgs e)
        {
            try
            {
                ToolStripDropDownItem source = (ToolStripDropDownItem)sender;
                switch (source.Text)
                {
                    case "Large Icons":
                    case "Lar&ge Icons":
                        lvListView.View = View.LargeIcon;
                        SetMnuView(largeIconsToolStripMenuItem1, smallIconsToolStripMenuItem1, viewList, viewDetails);
                        SetMnuView(largeIconsToolStripMenuItem, iconsToolStripMenuItem, listToolStripMenuItem, detailsToolStripMenuItem);
                        SetMnuView(mnuLargeIcons, mnuSmallIcons, mnuList, mnuDetails);
                        break;
                    case "Sm&all Icons":
                    case "Small Icons":
                        lvListView.View = View.SmallIcon;
                        SetMnuView(smallIconsToolStripMenuItem1, largeIconsToolStripMenuItem1, viewList, viewDetails);
                        SetMnuView(iconsToolStripMenuItem, largeIconsToolStripMenuItem, listToolStripMenuItem, detailsToolStripMenuItem);
                        SetMnuView(mnuSmallIcons, mnuLargeIcons, mnuList, mnuDetails);
                        break;
                    case "&List":
                    case "List":
                        lvListView.View = View.List;
                        SetMnuView(viewList, largeIconsToolStripMenuItem1, smallIconsToolStripMenuItem1, viewDetails);
                        SetMnuView(listToolStripMenuItem, largeIconsToolStripMenuItem, iconsToolStripMenuItem, detailsToolStripMenuItem);
                        SetMnuView(mnuList, mnuLargeIcons, mnuSmallIcons, mnuDetails);
                        break;
                    case "&Details":
                    case "Details":
                        lvListView.View = View.Details;
                        SetMnuView(viewDetails, largeIconsToolStripMenuItem1, smallIconsToolStripMenuItem1, viewList);
                        SetMnuView(detailsToolStripMenuItem, largeIconsToolStripMenuItem, iconsToolStripMenuItem, listToolStripMenuItem);
                        SetMnuView(mnuDetails, mnuLargeIcons, mnuSmallIcons, mnuList);
                        break;
                    default:
                        break;
                }
            }
            catch { }

        }

        private void SetMnuView(ToolStripMenuItem t1, ToolStripMenuItem t2, ToolStripMenuItem t3, ToolStripMenuItem t4)
        {
            t1.Checked = true;
            t2.Checked = false;
            t3.Checked = false;
            t4.Checked = false;
        }

        private void mnuExpandAll_Click(object sender, EventArgs e)
        {
            try
            {
                tvTreeView.SelectedNode.ExpandAll();
            }
            catch { }
        }

        private void mnuCollapse_Click(object sender, EventArgs e)
        {
            try
            {
                tvTreeView.SelectedNode.Collapse();
            }
            catch { }
        }

        private void mnuExpand_Click(object sender, EventArgs e)
        {
            if (mnuExpand.Text == "&Expand")
                tvTreeView.SelectedNode.Expand();
            else tvTreeView.SelectedNode.Collapse();
            
        }

        private void mnuDelete_Click(object sender, EventArgs e)
        {
            try 
            {
                if (lvListView.Focused)//delete o listview
                {
                    ListView.SelectedListViewItemCollection slic = lvListView.SelectedItems;
                    if (slic.Count == 1)
                    {
                        ListViewItem item = slic[0];
                        clsitem.DeleteItem(item, lvListView);
                    }
                    if (slic.Count > 1)
                    {
                        
                        DialogResult d = MessageBox.Show("Are you sure want to delete these " + slic.Count + " items?", "Confirm Muliple File Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                        if (d == DialogResult.Yes)
                        {
                            foreach (ListViewItem lvi in slic)
                            {
                                clsitem.DeleteItems(lvi, lvListView);
                            }
                            if (cls.Path == "Desktop\\" || cls.Path == "Desktop")
                            {
                                Program.fmain.tvTreeView.Nodes[0].Remove();
                                clsTreeView.CreateTreeView(Program.fmain.tvTreeView);
                                cls.GetDesktop(Program.fmain.tvTreeView.Nodes[0], Program.fmain.lvListView);
                            }
                            else
                                cls.OpenFolder(cls.Path, lvListView);
                           statusLabel1.Text = lvListView.Items.Count.ToString() + " object(s)";
                        }
                    }
                }
                else if(tvTreeView.Focused)//delete o treeview
                {
                    TreeNode dir = tvTreeView.SelectedNode;
                   // string pathDir = dir.FullPath;
                    string pathDir = cboPath.Text.Trim();
                    if (!Microsoft.VisualBasic.FileIO.FileSystem.DirectoryExists(pathDir)) return;
                    DialogResult d = MessageBox.Show("Are you sure want to remove the folder '" +dir.Text + "' and all its\ncontents?", "Confirm Folder Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                    if (d == DialogResult.Yes)
                    {
                        Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory(pathDir, Microsoft.VisualBasic.FileIO.DeleteDirectoryOption.DeleteAllContents);
                        //Class.clsTreeView.CreateTreeView(tvTreeView);
                        tvTreeView.Nodes.Remove(dir);
                    }
                    else return;
                    
                }
            }
            catch { }
        }
       
        public bool isDir = false; public string sourceDir; public string sourceFile;
       
        public string[] arrSrceDir; public string strDest; public string strDestDir;
       
        public string folder; public ListViewItem item;
       
        public bool isCopy;
        public string[] sourceArr, destArr,folderArr;
        public bool[] isDirArr;
        public ListViewItem[] items;
        private void mnuCopy_Click(object sender, EventArgs e)
        {
            isCopy = true;
            try
            {
                if (!cboPath.Text.Trim().EndsWith("\\")) cboPath.Text += "\\";
                string path = cboPath.Text.Trim();
                char[] arr ={ '\\' };
                string[] nameList = cboPath.Text.Split(arr);
                string node1 = nameList.GetValue(1).ToString();
                if (nameList.GetValue(0).ToString() == "Desktop" && node1 != "My Documents" && node1 != "My Computer")
                {
                    path = SpecialDirectories.Desktop + "\\";
                    for (int i = 2; i < nameList.Length; i++)
                        path += nameList[i] + "\\";
                }
                //xem copy folder o listview hay la treeview
                if (lvListView.Focused)//copy o listview
                {
                    int i = 0;
                    ListView.SelectedListViewItemCollection slic = lvListView.SelectedItems;
                    int count = slic.Count;
                    if (count > 0)
                    {
                        items = new ListViewItem[count];
                        sourceArr = new string[count];
                        isDirArr = new bool[count];
                        folderArr = new string[count];
                        foreach (ListViewItem lvi in slic)
                        {
                            sourceArr[i] = path + lvi.Text;
                            if (lvi.SubItems[1].Text.Trim() == "File Folder")//Copy folder
                            {
                                isDirArr[i] = true;
                                folderArr[i] = sourceArr[i].Split('\\')[sourceArr[i].Split('\\').Length - 1];
                            }
                            else//copy file
                            {
                                isDirArr[i] = false;
                                items[i] = lvi;
                            }
                            i++;
                        }
                    }
                }
                else //copy folder o treeview (luon chi copy 1 item)
                {
                    sourceArr = new string[1];
                    isDirArr = new bool[1];
                    folderArr = new string[1];
                    isDirArr[0] = true;
                    sourceArr[0] = path;
                    folderArr[0] = tvTreeView.SelectedNode.Text;
                    //clsitem.strSrceDir = sourceDir;
                }
                cmnuPaste.Enabled = true;
                cmnutvPaste.Enabled = true;
            }
            catch { }

        }
        
        //Chua xu li duoc : copy xong roi ma chua refresh lai treeview duoc.
        private void mnuPaste_Click(object sender, EventArgs e)
        {
            try
            {
                
                if (!cboPath.Text.Trim().EndsWith("\\")) cboPath.Text += "\\";
                string path = cboPath.Text.Trim();
                char[] arr ={ '\\' };
                string[] nameList = cboPath.Text.Split(arr);
                string node1 = nameList.GetValue(1).ToString();
                if (nameList.GetValue(0).ToString() == "Desktop" && node1 != "My Documents" && node1 != "My Computer")
                {
                    path = SpecialDirectories.Desktop + "\\";
                    for (int i = 2; i < nameList.Length; i++)
                        path += nameList[i] + "\\";
                }
                strDestDir = path;
                if (isCopy == true)//copy/paste
                {
                    tCopy = new Thread(tsCopy);
                    tCopy.Start();
                    if (sourceArr.Length > 0)
                    {
                        for (int i = 0; i < sourceArr.Length; i++)
                        {
                            if (isDirArr[i])//copy folder
                            {
                                strDest = strDestDir + folderArr[i];
                                Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory(sourceArr[i], strDest);

                            }
                            else 
                            {
                                string dest = strDestDir + items[i].Text.Trim();
                                Microsoft.VisualBasic.FileIO.FileSystem.CopyFile(sourceArr[i], dest);
                            }
                        }
                    }
                    tCopy.Abort();
                }
                
                else //move/paste
                {
                    tMove = new Thread(tsMove);
                    tMove.Start();
                    if (sourceArr.Length > 0)
                    {
                        string[] strDestArr = new string[sourceArr.Length];
                        for (int i = 0; i < sourceArr.Length; i++)
                        {
                            if (isDirArr[i])//move folder
                            {
                                strDest = strDestDir + folderArr[i];
                                Microsoft.VisualBasic.FileIO.FileSystem.MoveDirectory(sourceArr[i], strDest);
                            }
                            else
                            {
                                string dest = strDestDir + items[i].Text.Trim();
                                Microsoft.VisualBasic.FileIO.FileSystem.MoveFile(sourceArr[i], dest);
                            }
                        }
                    }
                    tMove.Abort();
                }
              
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
            if (!cboPath.Text.Trim().EndsWith("\\")) cboPath.Text += "\\";
            string destDir = cboPath.Text.Trim();
            string[] nameListNew = cboPath.Text.Split('\\');
            string nodeNew1 = nameListNew.GetValue(1).ToString();
            if (nameListNew.GetValue(0).ToString() == "Desktop" && nodeNew1 != "My Documents" && nodeNew1 != "My Computer")
            {
                destDir = SpecialDirectories.Desktop + "\\";
                for (int i = 2; i < nameListNew.Length; i++)
                    destDir += nameListNew[i] + "\\";
            }
            if (cboPath.Text == "Desktop\\")
            {
                cls.GetDesktop(tvTreeView.Nodes[0], lvListView);
            }
            else
                cls.OpenFolder(destDir, lvListView);
            tvTreeView.Nodes[0].Remove();
            clsTreeView.CreateTreeView(tvTreeView);
            statusLabel1.Text = lvListView.Items.Count.ToString() + " object(s)";
        }
        
        private void mnuEdit_Click(object sender, EventArgs e)
        {
            if (sourceArr != null)
            {
                mnuPaste.Enabled = true;
            }
            else
            {
                mnuPaste.Enabled = false;
            }
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            Application.ExitThread();
        }

        private void lvListView_ColumnClick(object sender, ColumnClickEventArgs e)
        {
             this.lvListView.ListViewItemSorter = new clsListViewItemComparer(e.Column);
        }

        private void sbtnBack_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            try
            {
                string item = e.ClickedItem.Text.ToString();
                string path = History.BackItem(item);
                cls.OpenHistory(path, lvListView, tvTreeView.Nodes[0]);
            }
            catch { }
        }
        public int i=2;
        private void btnNewFolder_Click(object sender, EventArgs e)
        {
            try
            {
                if (!cboPath.Text.Trim().EndsWith("\\")) cboPath.Text += "\\";
                string path = cboPath.Text.Trim();
                char[] arr ={ '\\' };
                string[] nameList = cboPath.Text.Split(arr);
                string node1 = nameList.GetValue(1).ToString();
                if (nameList.GetValue(0).ToString() == "Desktop" && node1 != "My Documents" && node1 != "My Computer")
                {
                    path = SpecialDirectories.Desktop + "\\";
                    for (int i = 2; i < nameList.Length; i++)
                        path += nameList[i] + "\\";
                }
                string path0 = path + "New Folder";
                if (!Microsoft.VisualBasic.FileIO.FileSystem.DirectoryExists(path0))
                {
                    Microsoft.VisualBasic.FileIO.FileSystem.CreateDirectory(path0);
                }
                else
                {
                    while (i > 0)
                    {
                        string path1 = path + "New Folder(" + i.ToString() + ")";
                        if (!Microsoft.VisualBasic.FileIO.FileSystem.DirectoryExists(path1))
                        {
                            Microsoft.VisualBasic.FileIO.FileSystem.CreateDirectory(path1);
                            i = 2;
                            break;
                        }
                        i++;
                    }
                }
                if (path == SpecialDirectories.Desktop + "\\")
                {
                    tvTreeView.Nodes[0].Remove();
                    clsTreeView.CreateTreeView(tvTreeView);
                    cls.GetDesktop(tvTreeView.Nodes[0], lvListView);
                }
                else
                    cls.OpenFolder(cboPath.Text, lvListView);
                statusLabel1.Text = lvListView.Items.Count.ToString() + " object(s)";
            }
            catch { }
        }

        private void mnuCopyTo_Click(object sender, EventArgs e)
        {
            string destDir;
            FolderBrowserDialog fb = new FolderBrowserDialog();
            fb.Description = "Chọn đích cần copy đến :";
            fb.ShowDialog();
            try
            {
                if (!cboPath.Text.Trim().EndsWith("\\")) cboPath.Text += "\\";
                string path = cboPath.Text.Trim();
                char[] arr ={ '\\' };
                string[] nameList = cboPath.Text.Split(arr);
                string node1 = nameList.GetValue(1).ToString();
                if (nameList.GetValue(0).ToString() == "Desktop" && node1 != "My Documents" && node1 != "My Computer")
                {
                    path = SpecialDirectories.Desktop + "\\";
                    for (int i = 2; i < nameList.Length; i++)
                        path += nameList[i] + "\\";
                }
                if (lvListView.Focused)//copy o listview
                {
                    int i = 0;
                    ListView.SelectedListViewItemCollection slic = lvListView.SelectedItems;
                    int count = slic.Count;
                    if (count > 0)
                    {
                        items = new ListViewItem[count];
                        sourceArr = new string[count];
                        isDirArr = new bool[count];
                        folderArr = new string[count];
                        foreach (ListViewItem lvi in slic)
                        {
                            sourceArr[i] = path + lvi.Text;
                            if (lvi.SubItems[1].Text.Trim() == "File Folder")//Copy folder
                            {
                                isDirArr[i] = true;
                                folderArr[i] = sourceArr[i].Split('\\')[sourceArr[i].Split('\\').Length - 1];
                            }
                            else//copy file
                            {
                                isDirArr[i] = false;
                                items[i] = lvi;
                            }
                            i++;
                        }
                    }
                }
                else
                {
                    sourceArr = new string[1];
                    isDirArr = new bool[1];
                    folderArr = new string[1];
                    isDirArr[0] = true;
                    sourceArr[0] = path;
                    folderArr[0] = tvTreeView.SelectedNode.Text;
                }
                destDir = fb.SelectedPath + "\\";
                tCopy = new Thread(tsCopy);
                tCopy.Start();
                if (destDir != "" && sourceArr.Length > 0)
                {
                    for (int i = 0; i < sourceArr.Length; i++)
                    {
                        if (isDirArr[i])//copy folder
                        {
                            strDest = destDir + folderArr[i];
                            Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory(sourceArr[i], strDest);

                        }
                        else
                        {
                            string dest = destDir + items[i].Text.Trim();
                            Microsoft.VisualBasic.FileIO.FileSystem.CopyFile(sourceArr[i], dest);
                        }
                    }
                }
                tCopy.Abort();
            }

            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            tvTreeView.Nodes[0].Remove();
            clsTreeView.CreateTreeView(tvTreeView);
        }

        public TreeNode nodeCurrent;
        public ListViewItem lviCurrent;
        
        private void mnuCut_Click(object sender, EventArgs e)
        {
            isCopy = false;
            try
            {
                if (!cboPath.Text.Trim().EndsWith("\\")) cboPath.Text += "\\";
                string path = cboPath.Text.Trim();
                char[] arr ={ '\\' };
                string[] nameList = cboPath.Text.Split(arr);
                string node1 = nameList.GetValue(1).ToString();
                if (nameList.GetValue(0).ToString() == "Desktop" && node1 != "My Documents" && node1 != "My Computer")
                {
                    path = SpecialDirectories.Desktop + "\\";
                    for (int i = 2; i < nameList.Length; i++)
                        path += nameList[i] + "\\";
                }
                //xem move folder o listview hay la treeview
                if (lvListView.Focused)//move o listview
                {
                    int i = 0;
                    ListView.SelectedListViewItemCollection slic = lvListView.SelectedItems;
                    int count = slic.Count;
                    if (count > 0)
                    {
                        //Bo danh dau tat ca item
                        foreach (ListViewItem lvi in lvListView.Items)
                        {
                            if (lvi.ForeColor == Color.Red) lvi.ForeColor = Color.Black;
                        }
                        //Danh dau nhung item duoc chon
                        foreach (ListViewItem lvi in slic)
                        {
                            lvi.ForeColor = Color.Red;
                        }
                        items = new ListViewItem[count];
                        sourceArr = new string[count];
                        isDirArr = new bool[count];
                        folderArr = new string[count];
                        foreach (ListViewItem lvi in slic)
                        {
                            sourceArr[i] = path + lvi.Text;
                            if (lvi.SubItems[1].Text.Trim() == "File Folder")//move folder
                            {
                                isDirArr[i] = true;
                                folderArr[i] = sourceArr[i].Split('\\')[sourceArr[i].Split('\\').Length - 1];
                            }
                            else//move file
                            {
                                isDirArr[i] = false;
                                items[i] = lvi;
                            }
                            i++;
                        }
                    }
                }
                else //move folder o treeview (luon chi move 1 item)
                {
                    sourceArr = new string[1];
                    isDirArr = new bool[1];
                    folderArr = new string[1];
                    nodeCurrent = tvTreeView.SelectedNode;
                    isDirArr[0] = true;
                    sourceArr[0] = path;
                    folderArr[0] = tvTreeView.SelectedNode.Text;
                }
                cmnuPaste.Enabled = true;
                cmnutvPaste.Enabled = true;
            }
            catch { }
        }

        private void mnuMoveTo_Click(object sender, EventArgs e)
        {
            string destDir;
            FolderBrowserDialog fb = new FolderBrowserDialog();
            fb.Description = "Chọn đích cần di chuyển đến :";
            fb.ShowDialog();
            ListView.SelectedListViewItemCollection slic = null;
            try
            {
                if (!cboPath.Text.Trim().EndsWith("\\")) cboPath.Text += "\\";
                string path = cboPath.Text.Trim();
                char[] arr ={ '\\' };
                string[] nameList = cboPath.Text.Split(arr);
                string node1 = nameList.GetValue(1).ToString();
                if (nameList.GetValue(0).ToString() == "Desktop" && node1 != "My Documents" && node1 != "My Computer")
                {
                    path = SpecialDirectories.Desktop + "\\";
                    for (int i = 2; i < nameList.Length; i++)
                        path += nameList[i] + "\\";
                }
                if (lvListView.Focused)//move o listview
                {
                    int i = 0;
                    slic = lvListView.SelectedItems;
                    int count = slic.Count;
                    if (count > 0)
                    {
                        items = new ListViewItem[count];
                        sourceArr = new string[count];
                        isDirArr = new bool[count];
                        folderArr = new string[count];
                        foreach (ListViewItem lvi in slic)
                        {
                            sourceArr[i] = path + lvi.Text;
                            if (lvi.SubItems[1].Text.Trim() == "File Folder")//move folder
                            {
                                isDirArr[i] = true;
                                folderArr[i] = sourceArr[i].Split('\\')[sourceArr[i].Split('\\').Length - 1];
                            }
                            else//move file
                            {
                                isDirArr[i] = false;
                                items[i] = lvi;
                            }
                            i++;
                        }
                    }
                }
                else //move folder o treeview
                {
                    sourceArr = new string[1];
                    isDirArr = new bool[1];
                    folderArr = new string[1];
                    isDirArr[0] = true;
                    sourceArr[0] = path;
                    folderArr[0] = tvTreeView.SelectedNode.Text;

                }
                destDir = fb.SelectedPath + "\\";
                tMove = new Thread(tsMove);
                tMove.Start();
                if (destDir != "" && sourceArr.Length > 0)
                {
                    for (int i = 0; i < sourceArr.Length; i++)
                    {
                        if (isDirArr[i])//move folder
                        {
                            strDest = destDir + folderArr[i];
                            Microsoft.VisualBasic.FileIO.FileSystem.MoveDirectory(sourceArr[i], strDest);

                        }
                        else
                        {
                            string dest = destDir + items[i].Text.Trim();
                            Microsoft.VisualBasic.FileIO.FileSystem.MoveFile(sourceArr[i], dest);
                        }

                    }
                    
                }
                tMove.Abort();
            }
            catch 
            {
            }
            if (slic != null)
            {
                foreach (ListViewItem lvi in lvListView.Items)
                {
                    if (slic.Contains(lvi)) lvListView.Items.Remove(lvi);
                }
                statusLabel1.Text = lvListView.Items.Count.ToString() + " object(s)";
            }
            tvTreeView.Nodes[0].Remove();
            clsTreeView.CreateTreeView(tvTreeView);
            statusLabel1.Text = lvListView.Items.Count.ToString() + " object(s)";
        }
        public string currentName, currentPath;
        private void mnuRename_Click(object sender, EventArgs e)
        {
            try
            {
                if (lvListView.Focused)
                {
                    lviCurrent = lvListView.FocusedItem;
                    currentName = lviCurrent.Text;
                    currentPath = cboPath.Text.Trim() + currentName;
                    if (lviCurrent.SubItems[1].Text.Trim() == "File Folder")//rename folder
                    {
                        isDir = true;
                    }
                    else
                    {
                        isDir = false;
                    }
                }
                else
                {
                    isDir = true;
                    nodeCurrent = tvTreeView.SelectedNode;
                    currentName = nodeCurrent.Text;
                    currentPath = cboPath.Text.Trim();
                }
                Forms.frmRename frename = new frmRename();
                frename.ShowDialog();
            }
            catch { }
        }

        private void lvListView_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (e.KeyChar == 13)//Enter
                {
                    ListViewItem item = lvListView.FocusedItem;
                    cls.OpenFolder(item, lvListView);
                    cboPath.Text = cls.Path;
                    Program.fmain.Text = item.Text;
                    cls.HistoryAdd();
                    statusLabel1.Text = lvListView.Items.Count.ToString() + " object(s)";
                }
                if (e.KeyChar == 8)//Up
                {
                    btnUp.PerformClick();
                }
            }
            catch (Exception ex) { MessageBox.Show(e.ToString()); }
        }

        private void mnuSelectAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < lvListView.Items.Count; i++)
            {
                lvListView.Items[i].Selected = true;
                lvListView.Focus();
            }
        }

        private void listViewDelete_Click(object sender, EventArgs e)
        {
            mnuDelete.PerformClick();
        }

        private void listViewMove_Click(object sender, EventArgs e)
        {
            mnuCut.PerformClick();
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mnuCopy.PerformClick();
        }

        private void listViewPaste_Click(object sender, EventArgs e)
        {
            mnuPaste.PerformClick();
        }

        private void treeviewContext_Opening(object sender, CancelEventArgs e)
        {
            if (tvTreeView.SelectedNode.IsExpanded)
            {
                cmnuExpand.Text = "&Collapse";
            }
            else
            {
                cmnuExpand.Text = "&Expand";
            }

        }

        private void expandAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tvTreeView.SelectedNode.ExpandAll();
        }

        private void toolStripMenuItem14_Click(object sender, EventArgs e)
        {
            mnuDelete.PerformClick();
        }

        private void toolStripMenuItem15_Click(object sender, EventArgs e)
        {
            mnuCut.PerformClick();
        }

        private void toolStripMenuItem16_Click(object sender, EventArgs e)
        {
            mnuCopy.PerformClick();
        }

        private void cmnutvPaste_Click(object sender, EventArgs e)
        {
            mnuPaste.PerformClick();
        }

        private void cmnuExpand_Click(object sender, EventArgs e)
        {
            if (cmnuExpand.Text == "&Collapse")
            {
                tvTreeView.SelectedNode.Collapse();
            }
            else
            {
                tvTreeView.SelectedNode.Expand();
            }
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            try
            {
                if (tvTreeView.SelectedNode.IsExpanded)
                {
                    mnuExpand.Text = "&Collapse";
                }
                else
                {
                    mnuExpand.Text = "&Expand";
                }
            }
            catch { }
        }
        public bool ispnSearchShow = false;
        private void label1_Click(object sender, EventArgs e)
        {
            if (lvListView.Columns.Count == 5) lvListView.Columns.RemoveAt(4);
            ispnSearchShow = false;
            reToolStripMenuItem.Enabled = true;
            btnNewFolder.Enabled = true;
            btnRefresh.Enabled = true;
            pnBase.Hide();
        }
        
        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                if (ispnSearchShow == false)
                {
                    pnBase.Show();
                    btnNewFolder.Enabled = false;
                    ispnSearchShow = true;
                    reToolStripMenuItem.Enabled = false;
                    btnRefresh.Enabled = false;
                    #region chon gia tri mac dinh cho cboPathSearch
                    if (cboPath.Text == "Desktop")
                    {
                        if (!cboPathSearch.Items.Contains(SpecialDirectories.Desktop))
                            cboPathSearch.Items.Add(SpecialDirectories.Desktop);
                    }
                    else
                    {
                        if (!cboPathSearch.Items.Contains(cboPath.Text.Trim()))
                            cboPathSearch.Items.Add(cboPath.Text.Trim());
                    }
                    if (cboPathSearch.Items.Count > 15) cboPathSearch.Items.RemoveAt(cboPathSearch.Items.Count - 4);
                    foreach (object o in cboPathSearch.Items)
                    {
                        if (cboPathSearch.DropDownWidth < o.ToString().Length * 6)
                            cboPathSearch.DropDownWidth = o.ToString().Length * 6;
                    }
                    cboPathSearch.SelectedItem = cboPathSearch.Items[cboPathSearch.Items.Count - 1];
                    cboPathSearch.Text = cboPathSearch.SelectedItem.ToString();
                    #endregion
                }
                else
                {
                    if (lvListView.Columns.Count == 5) lvListView.Columns.RemoveAt(4);
                    pnBase.Hide();
                    reToolStripMenuItem.Enabled = true;
                    btnNewFolder.Enabled = true;
                    btnRefresh.Enabled = true;
                    ispnSearchShow = false;
                }
                pnPicMusicVideo.Hide();
                pnDocuments.Hide();
                pnAllFileFolder.Hide();
                pnSearchOption.Show();
            }
            catch { }
        }
        public bool search = false;
        private void pnbtnSearch_Click(object sender, EventArgs e)
        {
            lvListView.Items.Clear();
            pnbtnBack.Enabled = false;
            pnbtnSearch.Enabled = false;
            try
            {
                string name = txtFileName.Text.Trim();
                int i = 0;
                if (lvListView.Columns.Count == 5)
                    lvListView.Columns.RemoveAt(4);
                lvListView.Columns.Add("In Folder", 300, HorizontalAlignment.Left);
                DirectoryInfo searchDir = new DirectoryInfo(pathSearch);
                switch (serchOption)
                {
                    case 3://Search all file or folder
                    default:                        
                        {
                            //clsSearch.searchDirectory(searchDir, name);
                            clsSearch.SearchFilePro(searchDir, name);
                            break;
                        }
                    case 2://search documents
                        {
                            DateTime dt = DateTime.Now;
                            TimeSpan ts;
                            DateTime dtModified;
                            string fileType = ".doc.pdf.txt.htm.html.rtf.prc.cpp";
                            if (rbLastWeek.Checked)
                            {
                                ts = new TimeSpan(7, 0, 0, 0);
                                dtModified = dt.Subtract(ts);
                                clsSearch.SearchFile(searchDir, name, dtModified,fileType);
                            }
                            else if (rbPastMonth.Checked)
                            {
                                ts = new TimeSpan(31, 0, 0, 0);
                                dtModified = dt.Subtract(ts);
                                clsSearch.SearchFile(searchDir, name, dtModified,fileType);
                            }
                            else if (rbLastYear.Checked)
                            {
                                ts = new TimeSpan(365, 0, 0, 0);
                                dtModified = dt.Subtract(ts);
                                clsSearch.SearchFile(searchDir, name, dtModified,fileType);
                            }
                            else
                            {
                                clsSearch.SearchFile(searchDir, name, fileType);
                            }

                            break;
                        }
                    case 1://search pictures, music and video
                        {
                            string typePic = ".jpg.jpeg.bmp.png.gif.tif";
                            string typeVideo = ".wmv.dat.flv.avi.mpeg.rm";
                            string typeMusic = ".wma.wav.mp3.mid.asf.mp2.mp4.cda";
                            if (cbPicPhoto.Checked)
                            {
                                clsSearch.SearchFile(searchDir, name, typePic);
                            }
                            if (cbVideo.Checked)
                            {
                                clsSearch.SearchFile(searchDir, name,typeVideo );
                            }
                            if (cbMusic.Checked)
                            {
                                clsSearch.SearchFile(searchDir, name, typeMusic);
                            }
                            if (!cbMusic.Checked && !cbPicPhoto.Checked && !cbVideo.Checked)
                            {
                                clsSearch.SearchFile(searchDir, name, typeMusic + typeVideo + typePic);
                            }
                            break;
                        }
                        
                }
            }
            catch { }
            if (lvListView.Items.Count == 0)
            {
                lvListView.Items.Add("Search is complete.");
                lvListView.Items.Add("There are no results");
                lvListView.Items.Add("to display.");
            }
            statusLabel1.Text = lvListView.Items.Count.ToString() + " object(s)";
            pnbtnBack.Enabled = true;
            pnbtnSearch.Enabled = true;
        }
        public string pathSearch = SpecialDirectories.MyDocuments;
        private void cboPathSearch_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                switch (cboPathSearch.SelectedItem.ToString())
                {
                    case "Browse...":
                        {
                            FolderBrowserDialog fb = new FolderBrowserDialog();
                            fb.Description = "Lua chon duong dan :";
                            fb.ShowDialog();
                            pathSearch = fb.SelectedPath;
                            if (pathSearch != "")
                            {
                                if (cboPathSearch.Items.Count == 13)
                                {
                                    cboPathSearch.Items.RemoveAt(11);
                                }
                                if (cboPathSearch.Items.Count < 13)
                                {
                                    cboPathSearch.Items.Add(pathSearch);
                                }
                                cboPathSearch.SelectedItem = cboPathSearch.Items[cboPathSearch.Items.Count - 1];
                            }
                            break;
                        }
                    case "My Documents":
                        {
                            pathSearch = SpecialDirectories.MyDocuments;
                            break;
                        }
                    case "My Pictures":
                        {
                            pathSearch = SpecialDirectories.MyPictures;
                            break;
                        }
                    case "Desktop":
                        {
                            pathSearch = SpecialDirectories.Desktop;
                            break;
                        }
                    case "Recent :":
                    case "My Computer":
                        MessageBox.Show("This is a keywork.\nPlease select one of the subentries in the list", "Select path", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        cboPathSearch.SelectedItem = cboPathSearch.Items[0];
                        return;
                        break;
                    case "    CD Drive (E:\\)":
                        pathSearch = "E:\\";
                        break;
                    case "    CD Drive (F:\\)":
                        pathSearch = "F:\\";
                        break;
                    case "    CD Drive (G:\\)":
                        pathSearch = "G:\\";
                        break;
                    case "    CD Drive (H:\\)":
                        pathSearch = "H:\\";
                        break;
                    case "    CD Drive (D:\\)":
                        pathSearch = "D:\\";
                        break;
                    default:
                        pathSearch = cboPathSearch.SelectedItem.ToString();
                        break;
                }
            }
            catch { }
        }

        private void label1_MouseLeave(object sender, EventArgs e)
        {
            label1.BorderStyle = BorderStyle.None;
        }

        private void label1_MouseDown(object sender, MouseEventArgs e)
        {
            label1.BorderStyle = BorderStyle.Fixed3D;
        }

        private void label1_MouseUp(object sender, MouseEventArgs e)
        {
            label1.BorderStyle = BorderStyle.None;
        }

        private void frmMain_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.F3)
                {
                    btnSearch.PerformClick();
                }
            }
            catch { }
        }

        private void searchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnSearch.PerformClick();
        }


        public int serchOption;
        private void llbPicMusicVideo_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                serchOption = 1;
                pnSearchOption.Hide();
                pnDocuments.Hide();
                pnAllFileFolder.Show();
                pnPicMusicVideo.Show();
            }
            catch { }
        }

        private void llbDocuments_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                serchOption = 2;
                pnSearchOption.Hide();
                pnPicMusicVideo.Hide();
                pnDocuments.Show();
                pnAllFileFolder.Show();
            }
            catch { }
        }

        private void llbAllFileFolder_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                serchOption = 3;
                pnSearchOption.Hide();
                pnPicMusicVideo.Hide();
                pnDocuments.Hide();
                pnAllFileFolder.Show();
            }
            catch { }
        }

        private void pnbtnBack_Click(object sender, EventArgs e)
        {
            try
            {
                btnSearch.PerformClick();
                btnSearch.PerformClick();
            }
            catch { }
        }
        public ListViewItem listviewitem1;
        public bool isMultiItems;
        public ListView.SelectedListViewItemCollection slic;
        private void cmnuProperties_Click(object sender, EventArgs e)
        {
            try
            {
                slic = lvListView.SelectedItems;
                if (slic.Count == 1)
                {
                    isMultiItems = false;
                    listviewitem1 = slic[0];
                    Program.fproperties.ShowDialog();
                }
                if (slic.Count > 1)
                {
                    isMultiItems = true;
                    Program.fproperties.ShowDialog();
                }
            }
            catch { }
        }

        private void lvcontextOpenContainingFolder_Click(object sender, EventArgs e)
        {
            try
            {
                ListViewItem lvi = lvListView.FocusedItem;
                string path = lvi.SubItems[4].Text.ToString();
                string[] patharr = path.Split('\\');
                string pathParent = path.Replace(patharr[patharr.Length - 1], "");
                DirectoryInfo di = new DirectoryInfo(pathParent);
                if (di.Exists) System.Diagnostics.Process.Start(pathParent);
            }
            catch { }
        }

        private void listViewContext_Opening(object sender, CancelEventArgs e)
        {
            try
            {
                if (lvListView.FocusedItem.Text == "My Computer" | lvListView.FocusedItem.Text == "Desktop" | lvListView.FocusedItem.Text == "My Documents" | lvListView.FocusedItem.SubItems[1].Text == "Fixed")
                {
                    lvcontextOpenContainingFolder.Enabled = false;
                    listViewDelete.Enabled = false;
                    cmnuProperties.Enabled = false;
                    listViewMove.Enabled = false;
                    copyToolStripMenuItem.Enabled = false;
                }
                else
                {
                    listViewDelete.Enabled = true;
                    cmnuProperties.Enabled = true;
                    lvcontextOpenContainingFolder.Enabled = true;
                    listViewMove.Enabled = true;
                    copyToolStripMenuItem.Enabled = true;
                }
                if (lvListView.FocusedItem.Selected)
                {
                    cmnuNew.Enabled = false;
                }
                else
                {
                    cmnuNew.Enabled = true;
                    cmnuProperties.Enabled = false;
                }
            }
            catch
            {
                lvcontextOpenContainingFolder.Enabled = false;
            }
        }

        private void txtFileName_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.Enter) pnbtnSearch.PerformClick();
            }
            catch { }
        }

        private void cboPath_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.Enter) btnGo.PerformClick();
            }
            catch { }
        }

        //Load cac dia chi thu muc trong vao cboPath
        private void cboPath_Enter(object sender, EventArgs e)
        {
            try
            {
                cboPath.Items.Clear();
                //cboPath.Items.Add("My Computer");
                listviewitem1 = lvListView.Items[0];
                string path = listviewitem1.SubItems[4].Text.ToString();
                DirectoryInfo di = new DirectoryInfo(path);
                DirectoryInfo dParent = di.Parent;
                if (dParent.Exists)
                {
                    
                    foreach (DirectoryInfo subdParent in dParent.GetDirectories())
                    {
                        cboPath.Items.Add(subdParent.FullName);
                    }
                    //foreach (FileInfo fi in dParent.GetFiles())
                    //{
                    //    cboPath.Items.Add(fi.FullName);
                    //}
                }
            }
            catch { }
        }

        private void cboPath_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnGo.PerformClick();
        }

        private void folderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnNewFolder.PerformClick();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (cboPath.Text == "MyComputer" | cboPath.Text == "My Computer") btnNewFolder.Enabled = false;
            else btnNewFolder.Enabled = true;
            if (cboPath.Text == "Desktop" | cboPath.Text == "MyComputer" | cboPath.Text == "My Computer" | ispnSearchShow == true)
                txtSearch.Hide();
            else txtSearch.Show();
            if (!txtSearch.Focused)
            {
                txtSearch.Text = "Search";
                txtSearch.Font = new Font("Tahoma", 8, FontStyle.Italic);
            }
        }

        private void txtSearch_Click(object sender, EventArgs e)
        {
            txtSearch.Text = "";
            txtSearch.Font = new Font("Tahoma", 8, FontStyle.Regular);
        }
 
        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (txtSearch.Text != "Search" )//&& txtSearch.Text != "")
                {
                    string path = cboPath.Text;
                    DirectoryInfo dir = new DirectoryInfo(path);
                    if (dir.Exists)
                    {
                        lvListView.Items.Clear();
                        foreach (DirectoryInfo subDir in dir.GetDirectories())
                        {
                            if (subDir.Name.ToUpper().Contains(txtSearch.Text.ToUpper()))
                            {
                                ListViewItem lvi = clsTreeView.CreatLVItem(subDir);
                                lvListView.Items.Add(lvi);
                            }
                        }
                        foreach (FileInfo fi in dir.GetFiles())
                        {
                            if (fi.Name.ToUpper().Contains(txtSearch.Text.ToUpper()))
                            {
                                ListViewItem lvi = clsTreeView.CreatLVItem(fi);
                                lvListView.Items.Add(lvi);
                            }
                        }
                    }
                }
            }
            catch { }
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down && lvListView.Items.Count > 0)
            {
                lvListView.Focus();
                lvListView.FocusedItem = lvListView.Items[0];
            }
        }

        private void lvListView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up && lvListView.FocusedItem == lvListView.Items[0])
                txtSearch.Focus();
        }

        //dung chuot keo 1 item trong listview
        
        //Hang so dung de kiem tra phim Ctrl co duoc nhan khi keo chuot ko
        const byte CtrlMask = 8;
        private void lvListView_DragDrop(object sender, DragEventArgs e)
        {
            //ListViewItem originationItem = (ListViewItem)e.Data.GetData("System.Windows.Forms.ListViewItem");
            if (e.Data.GetDataPresent("System.Windows.Forms.ListViewItem", false))
            {
                ListView.SelectedListViewItemCollection slic = new ListView.SelectedListViewItemCollection(lvListView);
                Point pt = ((ListView)sender).PointToClient(new Point(e.X, e.Y));
                ListViewItem destinationItem = ((ListView)sender).GetItemAt(pt.X, pt.Y);
                string pathDest = destinationItem.SubItems[4].Text;
                if (!new DirectoryInfo(pathDest).Exists) MessageBox.Show("Error Path");
                if ((e.KeyState & CtrlMask) != CtrlMask)//Phim Ctrl ko dc nhan -> move items
                {
                    tMove = new Thread(tsMove);
                    tMove.Start();
                    foreach (ListViewItem lvi in slic)
                    {
                        try
                        {
                            string pathSource = lvi.SubItems[4].Text;
                            string pathDestFull = pathDest + "\\" + lvi.SubItems[0].Text;
                            if (lvi.SubItems[1].Text == "File Folder")//folder
                            {
                                FileSystem.MoveDirectory(pathSource, pathDestFull);
                                lvListView.Items.Remove(lvi);
                            }
                            else if (new FileInfo(pathSource).Exists)//file
                            {
                                FileSystem.MoveFile(pathSource, pathDestFull, UIOption.AllDialogs);
                                lvListView.Items.Remove(lvi);
                            }
                        }
                        catch { }
                    }
                    statusLabel1.Text = lvListView.Items.Count.ToString() + " object(s)";
                    tMove.Abort();
                }
                else//Copy items
                {
                    tCopy = new Thread(tsCopy);
                    tCopy.Start();
                    foreach (ListViewItem lvi in slic)
                    {
                        try
                        {
                            string pathSource = lvi.SubItems[4].Text;
                            string pathDestFull = pathDest + "\\" + lvi.SubItems[0].Text;
                            if (lvi.SubItems[1].Text == "File Folder")//folder
                            {
                                FileSystem.CopyDirectory(pathSource, pathDestFull);
                            }
                            else if (new FileInfo(pathSource).Exists)//file
                            {
                                FileSystem.CopyFile(pathSource, pathDestFull, UIOption.AllDialogs);
                            }
                        }
                        catch { }
                    }
                    tCopy.Abort();
                }
            }
        }

        private void lvListView_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent("System.Windows.Forms.ListViewItem"))
            {
                if ((e.KeyState & CtrlMask) == CtrlMask)
                    e.Effect = DragDropEffects.Copy;
                else
                    e.Effect = DragDropEffects.Move;
            }
            else e.Effect = DragDropEffects.None;
        }

        private void lvListView_ItemDrag(object sender, ItemDragEventArgs e)
        {
            if (e.Button == MouseButtons.Left && cboPath.Text != "Desktop" & cboPath.Text != "MyComputer")
            {
                DoDragDrop(e.Item, DragDropEffects.Copy | DragDropEffects.Move);
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                tvTreeView.Nodes[0].Remove();
                lvListView.Items.Clear();
                clsTreeView.CreateTreeView(tvTreeView);
                string path = cboPath.Text;
                try
                {
                    if (path == "Desktop") cls.GetDesktop(tvTreeView.Nodes[0], lvListView);
                    else if (path == "MyComputer" | path == "My Computer") cls.GetAllDriver(lvListView);
                    else
                        cls.OpenFolder(path, lvListView); ;
                }
                catch { }
            }
            catch { }
        }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnRefresh.PerformClick();
        }
        public ThreadStart tsCopy = new ThreadStart(Copy);
        public Thread tCopy;
        public ThreadStart tsMove = new ThreadStart(Move);
        public Thread tMove;
        private static void Copy()
        {
            Forms.frmCopying f = new frmCopying();
            f.ShowDialog();
        }
        private static void Move()
        {
            Forms.frmMoving f = new frmMoving();
            f.ShowDialog();
        }

        private void reToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnRefresh.PerformClick();
        }

        private void mnuHelpTopics_Click(object sender, EventArgs e)
        {
            try
            {
                string path1 = SpecialDirectories.ProgramFiles + "\\WE\\helpTopic.chm";
                if (new FileInfo(path1).Exists)
                    System.Diagnostics.Process.Start(path1);
            }
            catch { };
        }
    }
}